package com.nana.framework.net
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	public class HTTPRequest
	{
		public static const IOERROR:String = "IOError";
		public static const HTTPSTATUS:String = "httpStatus";
		public static const TIMEOUT:String = "timeOut";
		
		public static var REQUEST_ERROR:String = "requestError";
		public static var OVER_TIME:String = "overTime";
		private static var instance:HTTPRequest;
		
		private var _timer:Timer;
		private var _httpUrl:String = "";
		
		public function HTTPRequest(){
			if (instance != null){
				throw new Error("实例化单例类出错-HTTPRequest");
			}
			this._timer = new Timer(1000);
			this._timer.start();
		}
		
		public static function getInstance():HTTPRequest{
			if (instance == null){
				instance = new HTTPRequest();
			}
			return instance;
		}
		
		public function getRequest(_arg1:String, _arg2:Object, _arg3:Function, _arg4:uint=60000, _arg5:Boolean=false, _arg6:String="text", _arg7:Object=null):void{
			this.request(URLRequestMethod.GET, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7);
		}
		
		public function postRequest(_arg1:String, _arg2:Object, _arg3:Function, _arg4:uint=60000, _arg5:Boolean=false, _arg6:String="text", _arg7:Object=null):void{
			this.request(URLRequestMethod.POST, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7);
		}
		
		private function request(_arg1:String, _arg2:String, _arg3:Object, _arg4:Function, _arg5:uint=60000, _arg6:Boolean=false, _arg7:String="text", _arg8:Object=null):void{
			var _postData:* = null;
			var isHandler:* = false;
			var aURLRequest:* = null;
			var aURLLoader:* = null;
			var ioerrorHandler:* = null;
			var httpStatusHandler:* = null;
			var loadCompHandler:* = null;
			var timerFn:* = null;
			var timerValue:* = 0;
			var requestNum:* = 0;
			var _uv:* = null;
			var _name:* = null;
			var method:* = _arg1;
			var url:* = _arg2;
			var value:* = _arg3;
			var handlerFun:* = _arg4;
			var timeout:int = _arg5;
			var reload:Boolean = _arg6;
			var dataFormat:String = _arg7;
			var parameter = _arg8;
			var completeUrl:* = "";
			if (/^http.*/.test(url)){
				completeUrl = url;
			} else {
				if (/^debug.*/.test(url)){
					completeUrl = url.replace("debug:", "");
				} else {
					completeUrl = (this.httpUrl + url);
				};
			};
			if ((value is String)){
				_postData = value;
			} else {
				if ((value is URLVariables)){
					_postData = value;
				} else {
					_uv = new URLVariables();
					for (_name in value) {
						_uv[_name] = value[_name];
					};
					_postData = _uv;
				}
			}
			isHandler = false;
			aURLRequest = new URLRequest(completeUrl);
			aURLRequest.method = method;
			aURLRequest.data = _postData;
			aURLLoader = new URLLoader();
			aURLLoader.dataFormat = dataFormat;
			var clearAll:* = function ():void{
				aURLLoader.close();
				_timer.removeEventListener(TimerEvent.TIMER, timerFn);
				aURLLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioerrorHandler);
				aURLLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
				aURLLoader.removeEventListener(Event.COMPLETE, loadCompHandler);
			}
			ioerrorHandler = function (_arg1:IOErrorEvent):void{
				if (!isHandler){
					if (parameter == null){
						handlerFun("{\"errorType\":\"IOError\",\"errorContent\":\"\"}");
					} else {
						handlerFun("{\"errorType\":\"IOError\",\"errorContent\":\"\"}", parameter);
					}
					isHandler = true;
				}
				clearAll();
			}
			httpStatusHandler = function (_arg1:HTTPStatusEvent):void{
				if (((!((_arg1.status == 0))) && (!((_arg1.status == 200))))){
					if (!isHandler){
						if (parameter == null){
							handlerFun((("{\"errorType\":\"httpStatus\",\"errorContent\":" + _arg1.status) + "}"));
						} else {
							handlerFun((("{\"errorType\":\"httpStatus\",\"errorContent\":" + _arg1.status) + "}"), parameter);
						}
						isHandler = true;
					}
				}
			}
			loadCompHandler = function (_arg1:Event):void{
				if (!isHandler){
					if (parameter == null){
						handlerFun(_arg1.currentTarget.data);
					} else {
						handlerFun(_arg1.currentTarget.data, parameter);
					}
					isHandler = true;
				}
				clearAll();
			}
			timerFn = function (_arg1:TimerEvent):void{
				if (reload){
					if (((((getTimer() - timerValue) > timeout)) && ((requestNum == 0)))){
						aURLLoader.load(aURLRequest);
						timerValue = getTimer();
						requestNum = 1;
					} else {
						if (((getTimer() - timerValue) > timeout) && (requestNum == 1)){
							clearAll();
							if (!isHandler){
								if (parameter == null){
									handlerFun("{\"errorType\":\"timeOut\",\"content\":\"\"}");
								} else {
									handlerFun("{\"errorType\":\"timeOut\",\"content\":\"\"}", parameter);
								}
								isHandler = true;
							}
						}
					}
				} else {
					if ((getTimer() - timerValue) > timeout){
						clearAll();
						if (!isHandler){
							if (parameter == null){
								handlerFun("{\"errorType\":\"timeOut\",\"content\":\"\"}");
							} else {
								handlerFun("{\"errorType\":\"timeOut\",\"content\":\"\"}", parameter);
							}
							isHandler = true;
						}
					}
				}
			}
			aURLLoader.addEventListener(IOErrorEvent.IO_ERROR, ioerrorHandler);
			aURLLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			aURLLoader.addEventListener(Event.COMPLETE, loadCompHandler);
			aURLLoader.load(aURLRequest);
			timerValue = getTimer();
			requestNum = 0;
			this._timer.addEventListener(TimerEvent.TIMER, timerFn);
		}

		public function get httpUrl():String
		{
			return _httpUrl;
		}

		public function set httpUrl(value:String):void
		{
			_httpUrl = value;
		}


	}
}